package anno;

import org.junit.Test;

import java.lang.annotation.*;
import java.lang.reflect.AnnotatedElement;

public class Annotest {


    @Target({ElementType.METHOD,ElementType.TYPE,ElementType.FIELD})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @interface TestAnna{
        String name() default "";
        int age() default 0;
    }

    @TestAnna(name = "class name")
    static class HasAnnoClass{
        @TestAnna(name = "filed name")
        private String name;
    }
    @Test
    public void testClassAnnno() throws Exception{
        TestAnna classRa = getAnnotation(HasAnnoClass.class,TestAnna.class);
        TestAnna fieldTa = getAnnotation(HasAnnoClass.class.getDeclaredField("name"),TestAnna.class);
        System.err.println(classRa.name());
        System.err.println(fieldTa.name());
    }

    public static <A extends Annotation> A getAnnotation(AnnotatedElement ae,Class<A> annotationClass){
        return ae.getAnnotation(annotationClass);
    }

}
